package com.wthink.hzgj.home.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.BusRepository
import com.wthink.hzgj.data.repository.DefaultBusRepository
import com.wthink.hzgj.home.data.Line
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.service.jt808.JT_0B07
import com.wthink.hzgj.util.Page
import com.wthink.hzgj.util.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HomeViewModel : ViewModel() {

    private val busRepository: BusRepository = DefaultBusRepository()

    val lineList = MutableLiveData(
        Page(
            emptyList<Line>(), 0, 1
        )
    )

    fun start() {
        viewModelScope.launch(Dispatchers.IO) {
            if (lineList.value!!.list.isEmpty()) {
                lineList.postValue(
                    busRepository.getLinePage(lineList.value!!.page, true)
                )
            }

            busRepository.refresh()?.let {
                it.join()

                lineList.postValue(
                    busRepository.getLinePage(lineList.value!!.page, true).apply {
                        val selectId = getSelectLine()?.id
                        if (selectId != null) {
                            list.find { it.id == selectId }?.select = true
                        }
                    }
                )
            }
        }
        waitBusState()
    }

    private fun loadLine(page: Int = 0) {
        viewModelScope.launch {
            lineList.postValue(busRepository.getLinePage(page, true))
        }
    }

    fun loadPrev() {
        loadLine(lineList.value!!.page - 1)
    }

    fun loadNext() {
        loadLine(lineList.value!!.page + 1)
    }

    fun selectLine(id: String) {
        val newLineList = lineList.value!!.list.map {
            Line(it.id, it.name, it.id.equals(id))
        }

        lineList.postValue(Page(newLineList, lineList.value!!.page, lineList.value!!.pageTotal))
    }

    fun getSelectLine(): Line? {
        val line = lineList.value!!.list.find { it.select }
        return line
    }

    fun getSelectLine1(): Line? {
        val line = lineList.value!!.list.find { it.select }
        if (line == null) {
            toast(BusApplication.busContext, "请选择线路")
        }
        return line
    }

    private fun waitBusState() {
        viewModelScope.launch(Dispatchers.IO) {
            DevService.listenBusState().collect {
                selectLine(it.lineId)
            }
        }
    }

    fun reqLine() {
        viewModelScope.launch(Dispatchers.IO) {
            val ret = DevService.send808(JT_0B07("12345"))
            withContext(Dispatchers.Main) {
                if (!ret.success) {
                    toast(BusApplication.busContext, ret.err.toString())
                } else {
                    toast(BusApplication.busContext, "请求行车计划成功")
                }
            }
        }
    }
}
